/*   1:    */ package com.atomikos.icatch.jta;
/*   2:    */ 
/*   3:    */ import com.atomikos.icatch.CompositeTransaction;
/*   4:    */ import com.atomikos.icatch.CompositeTransactionManager;
/*   5:    */ import com.atomikos.icatch.SubTxAwareParticipant;
/*   6:    */ import com.atomikos.icatch.SysException;
/*   7:    */ import com.atomikos.icatch.TransactionControl;
/*   8:    */ import com.atomikos.icatch.TxState;
/*   9:    */ import com.atomikos.icatch.imp.ResumePreviousTransactionSubTxAwareParticipant;
/*  10:    */ import com.atomikos.icatch.system.Configuration;
/*  11:    */ import java.util.Hashtable;
/*  12:    */ import java.util.Stack;
/*  13:    */ import javax.naming.NamingException;
/*  14:    */ import javax.naming.Reference;
/*  15:    */ import javax.naming.Referenceable;
/*  16:    */ import javax.naming.StringRefAddr;
/*  17:    */ import javax.transaction.HeuristicMixedException;
/*  18:    */ import javax.transaction.HeuristicRollbackException;
/*  19:    */ import javax.transaction.InvalidTransactionException;
/*  20:    */ import javax.transaction.NotSupportedException;
/*  21:    */ import javax.transaction.RollbackException;
/*  22:    */ import javax.transaction.SystemException;
/*  23:    */ import javax.transaction.Transaction;
/*  24:    */ import javax.transaction.TransactionManager;
/*  25:    */ import javax.transaction.UserTransaction;
/*  26:    */ 
/*  27:    */ public class TransactionManagerImp
/*  28:    */   implements TransactionManager, SubTxAwareParticipant, Referenceable, UserTransaction
/*  29:    */ {
/*  30:    */   private static final long serialVersionUID = -3048879409985542685L;
/*  31:    */   public static final String JTA_PROPERTY_NAME = "com.atomikos.icatch.jta.transaction";
/*  32: 73 */   private static TransactionManagerImp singleton_ = null;
/*  33:    */   private static int defaultTimeout;
/*  34: 85 */   private static boolean default_serial = false;
/*  35:    */   private int timeout_;
/*  36:    */   private Hashtable txmap_;
/*  37:    */   private CompositeTransactionManager ctm_;
/*  38:    */   private int count_;
/*  39:    */   private boolean automaticResourceRegistration_;
/*  40:    */   
/*  41:    */   private static final void raiseNoTransaction()
/*  42:    */   {
/*  43:113 */     StringBuffer msg = new StringBuffer();
/*  44:114 */     msg.append("This method needs a transaction for the calling thread and none exists.\n");
/*  45:115 */     msg.append("Possible causes: either you didn't start a transaction,\n");
/*  46:116 */     msg.append("it rolledback due to timeout, or it was committed already.\n");
/*  47:117 */     msg.append("ACTIONS: You can try one of the following: \n");
/*  48:118 */     msg.append("1. Make sure you started a transaction for the thread.\n");
/*  49:119 */     msg.append("2. Make sure you didn't terminate it yet.\n");
/*  50:120 */     msg.append("3. Increase the transaction timeout to avoid automatic rollback of long transactions;\n");
/*  51:121 */     msg.append("   check http://www.atomikos.com/Documentation/JtaProperties for how to do this.");
/*  52:122 */     Configuration.logWarning(msg.toString());
/*  53:123 */     throw new IllegalStateException(msg.toString());
/*  54:    */   }
/*  55:    */   
/*  56:    */   public static void setDefaultSerial(boolean serial)
/*  57:    */   {
/*  58:135 */     default_serial = serial;
/*  59:    */   }
/*  60:    */   
/*  61:    */   public static boolean getDefaultSerial()
/*  62:    */   {
/*  63:147 */     return default_serial;
/*  64:    */   }
/*  65:    */   
/*  66:    */   public static void setDefaultTimeout(int defaultTimeoutValue)
/*  67:    */   {
/*  68:158 */     defaultTimeout = defaultTimeoutValue;
/*  69:    */   }
/*  70:    */   
/*  71:    */   public static int getDefaultTimeout()
/*  72:    */   {
/*  73:168 */     return defaultTimeout;
/*  74:    */   }
/*  75:    */   
/*  76:    */   public static synchronized void installTransactionManager(CompositeTransactionManager ctm, boolean automaticResourceRegistration)
/*  77:    */   {
/*  78:189 */     if (ctm == null) {
/*  79:190 */       singleton_ = null;
/*  80:    */     } else {
/*  81:192 */       singleton_ = new TransactionManagerImp(ctm, automaticResourceRegistration);
/*  82:    */     }
/*  83:    */   }
/*  84:    */   
/*  85:    */   public static TransactionManager getTransactionManager()
/*  86:    */   {
/*  87:205 */     return singleton_;
/*  88:    */   }
/*  89:    */   
/*  90:    */   private TransactionManagerImp(CompositeTransactionManager ctm, boolean automaticResourceRegistration)
/*  91:    */   {
/*  92:219 */     this.ctm_ = ctm;
/*  93:220 */     this.count_ = 0;
/*  94:221 */     this.timeout_ = defaultTimeout;
/*  95:222 */     this.txmap_ = new Hashtable();
/*  96:223 */     this.automaticResourceRegistration_ = automaticResourceRegistration;
/*  97:    */   }
/*  98:    */   
/*  99:    */   private void addToMap(String tid, TransactionImp tx)
/* 100:    */   {
/* 101:228 */     synchronized (this.txmap_)
/* 102:    */     {
/* 103:229 */       this.txmap_.put(tid.toString(), tx);
/* 104:    */     }
/* 105:    */   }
/* 106:    */   
/* 107:    */   private void removeFromMap(String tid)
/* 108:    */   {
/* 109:235 */     synchronized (this.txmap_)
/* 110:    */     {
/* 111:236 */       this.txmap_.remove(tid.toString());
/* 112:    */     }
/* 113:    */   }
/* 114:    */   
/* 115:    */   private CompositeTransaction getCompositeTransaction()
/* 116:    */     throws ExtendedSystemException
/* 117:    */   {
/* 118:242 */     CompositeTransaction ct = null;
/* 119:    */     try
/* 120:    */     {
/* 121:244 */       ct = this.ctm_.getCompositeTransaction();
/* 122:    */     }
/* 123:    */     catch (SysException se)
/* 124:    */     {
/* 125:246 */       String msg = "Error while retrieving the transaction for the calling thread";
/* 126:247 */       Configuration.logWarning(msg, se);
/* 127:248 */       throw new ExtendedSystemException(msg, se.getErrors());
/* 128:    */     }
/* 129:251 */     return ct;
/* 130:    */   }
/* 131:    */   
/* 132:    */   TransactionImp getPreviousInstance(String tid)
/* 133:    */   {
/* 134:265 */     synchronized (this.txmap_)
/* 135:    */     {
/* 136:266 */       if (this.txmap_.containsKey(tid.toString())) {
/* 137:267 */         return (TransactionImp)this.txmap_.get(tid.toString());
/* 138:    */       }
/* 139:269 */       return null;
/* 140:    */     }
/* 141:    */   }
/* 142:    */   
/* 143:    */   public Transaction getTransaction(String tid)
/* 144:    */   {
/* 145:283 */     return getPreviousInstance(tid);
/* 146:    */   }
/* 147:    */   
/* 148:    */   public void begin()
/* 149:    */     throws NotSupportedException, SystemException
/* 150:    */   {
/* 151:300 */     begin(this.timeout_);
/* 152:    */   }
/* 153:    */   
/* 154:    */   public void begin(int timeout)
/* 155:    */     throws NotSupportedException, SystemException
/* 156:    */   {
/* 157:310 */     TransactionImp tx = null;
/* 158:311 */     CompositeTransaction ct = null;
/* 159:312 */     ResumePreviousTransactionSubTxAwareParticipant resumeParticipant = null;
/* 160:    */     
/* 161:314 */     ct = this.ctm_.getCompositeTransaction();
/* 162:315 */     if ((ct != null) && (ct.getProperty("com.atomikos.icatch.jta.transaction") == null))
/* 163:    */     {
/* 164:316 */       Configuration.logWarning("JTA: temporarily suspending incompatible transaction: " + ct.getTid() + " (will be resumed after JTA transaction ends)");
/* 165:    */       
/* 166:318 */       ct = this.ctm_.suspend();
/* 167:319 */       resumeParticipant = new ResumePreviousTransactionSubTxAwareParticipant(ct);
/* 168:    */     }
/* 169:    */     try
/* 170:    */     {
/* 171:323 */       ct = this.ctm_.createCompositeTransaction(timeout * 1000L);
/* 172:324 */       if (resumeParticipant != null) {
/* 173:324 */         ct.addSubTxAwareParticipant(resumeParticipant);
/* 174:    */       }
/* 175:325 */       if ((ct.isRoot()) && (getDefaultSerial())) {
/* 176:326 */         ct.getTransactionControl().setSerial();
/* 177:    */       }
/* 178:327 */       ct.setProperty("com.atomikos.icatch.jta.transaction", "true");
/* 179:    */     }
/* 180:    */     catch (SysException se)
/* 181:    */     {
/* 182:329 */       String msg = "Error in begin()";
/* 183:330 */       Configuration.logWarning(msg, se);
/* 184:331 */       throw new ExtendedSystemException(msg, se.getErrors());
/* 185:    */     }
/* 186:336 */     tx = new TransactionImp(ct, this.automaticResourceRegistration_);
/* 187:337 */     addToMap(ct.getTid(), tx);
/* 188:338 */     ct.addSubTxAwareParticipant(this);
/* 189:    */   }
/* 190:    */   
/* 191:    */   public Transaction getTransaction()
/* 192:    */     throws SystemException
/* 193:    */   {
/* 194:348 */     TransactionImp ret = null;
/* 195:349 */     CompositeTransaction ct = null;
/* 196:350 */     ct = getCompositeTransaction();
/* 197:352 */     if ((ct == null) || (ct.getProperty("com.atomikos.icatch.jta.transaction") == null))
/* 198:    */     {
/* 199:353 */       ret = null;
/* 200:    */     }
/* 201:    */     else
/* 202:    */     {
/* 203:358 */       ret = getPreviousInstance(ct.getTid());
/* 204:359 */       if ((ret == null) && (ct.getState().equals(TxState.ACTIVE)))
/* 205:    */       {
/* 206:361 */         ret = new TransactionImp(ct, this.automaticResourceRegistration_);
/* 207:362 */         addToMap(ct.getTid(), ret);
/* 208:363 */         ct.addSubTxAwareParticipant(this);
/* 209:    */       }
/* 210:    */     }
/* 211:367 */     return ret;
/* 212:    */   }
/* 213:    */   
/* 214:    */   public void setTransactionTimeout(int seconds)
/* 215:    */     throws SystemException
/* 216:    */   {
/* 217:379 */     if (seconds > 0)
/* 218:    */     {
/* 219:380 */       this.timeout_ = seconds;
/* 220:    */     }
/* 221:381 */     else if (seconds == 0)
/* 222:    */     {
/* 223:382 */       this.timeout_ = defaultTimeout;
/* 224:    */     }
/* 225:    */     else
/* 226:    */     {
/* 227:384 */       String msg = "setTransactionTimeout: value must be >= 0";
/* 228:385 */       Configuration.logWarning(msg);
/* 229:386 */       throw new SystemException(msg);
/* 230:    */     }
/* 231:    */   }
/* 232:    */   
/* 233:    */   public int getTransactionTimeout()
/* 234:    */   {
/* 235:393 */     return this.timeout_;
/* 236:    */   }
/* 237:    */   
/* 238:    */   public Transaction suspend()
/* 239:    */     throws SystemException
/* 240:    */   {
/* 241:403 */     getTransaction();
/* 242:    */     
/* 243:405 */     TransactionImp ret = null;
/* 244:406 */     CompositeTransaction ct = null;
/* 245:    */     try
/* 246:    */     {
/* 247:408 */       ct = this.ctm_.suspend();
/* 248:    */     }
/* 249:    */     catch (SysException se)
/* 250:    */     {
/* 251:410 */       String msg = "Unexpected error while suspending the existing transaction for the current thread";
/* 252:411 */       Configuration.logWarning(msg, se);
/* 253:412 */       throw new ExtendedSystemException(msg, se.getErrors());
/* 254:    */     }
/* 255:415 */     if (ct != null)
/* 256:    */     {
/* 257:417 */       ret = getPreviousInstance(ct.getTid());
/* 258:418 */       if (ret != null) {
/* 259:420 */         ret.suspendEnlistedXaResources();
/* 260:    */       }
/* 261:    */     }
/* 262:424 */     return ret;
/* 263:    */   }
/* 264:    */   
/* 265:    */   public void resume(Transaction tobj)
/* 266:    */     throws InvalidTransactionException, IllegalStateException, SystemException
/* 267:    */   {
/* 268:434 */     if ((tobj == null) || (!(tobj instanceof TransactionImp)))
/* 269:    */     {
/* 270:435 */       String msg = "The specified transaction object is invalid for this configuration: " + tobj;
/* 271:436 */       Configuration.logWarning(msg);
/* 272:437 */       throw new InvalidTransactionException(msg);
/* 273:    */     }
/* 274:440 */     TransactionImp tximp = (TransactionImp)tobj;
/* 275:    */     try
/* 276:    */     {
/* 277:442 */       this.ctm_.resume(tximp.getCT());
/* 278:    */     }
/* 279:    */     catch (SysException se)
/* 280:    */     {
/* 281:444 */       String msg = "Unexpected error while resuming the transaction in the calling thread";
/* 282:445 */       Configuration.logWarning(msg, se);
/* 283:446 */       throw new ExtendedSystemException(msg, se.getErrors());
/* 284:    */     }
/* 285:    */   }
/* 286:    */   
/* 287:    */   public int getStatus()
/* 288:    */     throws SystemException
/* 289:    */   {
/* 290:458 */     int ret = 6;
/* 291:    */     
/* 292:    */ 
/* 293:461 */     getTransaction();
/* 294:    */     
/* 295:463 */     TransactionImp tx = null;
/* 296:464 */     CompositeTransaction ct = getCompositeTransaction();
/* 297:466 */     if (ct == null)
/* 298:    */     {
/* 299:467 */       ret = 6;
/* 300:    */     }
/* 301:    */     else
/* 302:    */     {
/* 303:469 */       tx = getPreviousInstance(ct.getTid());
/* 304:470 */       ret = tx.getStatus();
/* 305:    */     }
/* 306:473 */     return ret;
/* 307:    */   }
/* 308:    */   
/* 309:    */   public void commit()
/* 310:    */     throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException, IllegalStateException, SecurityException
/* 311:    */   {
/* 312:486 */     TransactionImp tx = null;
/* 313:487 */     CompositeTransaction ct = null;
/* 314:    */     
/* 315:    */ 
/* 316:490 */     getTransaction();
/* 317:    */     
/* 318:492 */     ct = getCompositeTransaction();
/* 319:494 */     if (ct == null)
/* 320:    */     {
/* 321:495 */       raiseNoTransaction();
/* 322:    */     }
/* 323:    */     else
/* 324:    */     {
/* 325:497 */       tx = getPreviousInstance(ct.getTid());
/* 326:498 */       tx.commit();
/* 327:    */     }
/* 328:    */   }
/* 329:    */   
/* 330:    */   public void rollback()
/* 331:    */     throws IllegalStateException, SystemException, SecurityException
/* 332:    */   {
/* 333:509 */     TransactionImp tx = null;
/* 334:510 */     CompositeTransaction ct = null;
/* 335:    */     
/* 336:    */ 
/* 337:513 */     getTransaction();
/* 338:    */     
/* 339:515 */     ct = getCompositeTransaction();
/* 340:517 */     if (ct == null)
/* 341:    */     {
/* 342:518 */       raiseNoTransaction();
/* 343:    */     }
/* 344:    */     else
/* 345:    */     {
/* 346:520 */       tx = getPreviousInstance(ct.getTid());
/* 347:521 */       tx.rollback();
/* 348:    */     }
/* 349:    */   }
/* 350:    */   
/* 351:    */   public void setRollbackOnly()
/* 352:    */     throws IllegalStateException, SystemException
/* 353:    */   {
/* 354:532 */     Stack errors = new Stack();
/* 355:    */     
/* 356:534 */     Transaction tx = getTransaction();
/* 357:535 */     if (tx == null) {
/* 358:536 */       raiseNoTransaction();
/* 359:    */     }
/* 360:    */     try
/* 361:    */     {
/* 362:538 */       tx.setRollbackOnly();
/* 363:    */     }
/* 364:    */     catch (SecurityException se)
/* 365:    */     {
/* 366:541 */       errors.push(se);
/* 367:542 */       String msg = "Unexpected error during setRollbackOnly";
/* 368:543 */       Configuration.logWarning(msg, se);
/* 369:544 */       throw new ExtendedSystemException(msg, errors);
/* 370:    */     }
/* 371:    */   }
/* 372:    */   
/* 373:    */   public void committed(CompositeTransaction tx)
/* 374:    */   {
/* 375:561 */     removeFromMap(tx.getTid());
/* 376:    */   }
/* 377:    */   
/* 378:    */   public void rolledback(CompositeTransaction tx)
/* 379:    */   {
/* 380:571 */     removeFromMap(tx.getTid());
/* 381:    */   }
/* 382:    */   
/* 383:    */   public Reference getReference()
/* 384:    */     throws NamingException
/* 385:    */   {
/* 386:586 */     return new Reference(getClass().getName(), new StringRefAddr("name", "TransactionManager"), TransactionManagerFactory.class.getName(), null);
/* 387:    */   }
/* 388:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions-jta\3.7.0\transactions-jta-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.jta.TransactionManagerImp
 * JD-Core Version:    0.7.0.1
 */